<%@ page import="foo.bar.wiki.security.UserAccountRealm" %>
<%@include file="/WEB-INF/pages/commons/commons.jsp" %>

<stripes:layout-render name="${_themePath}/basic-page-layout.jsp" _title="Page [${actionBean.page.name}]">

    <stripes:layout-component name="_head">
        <c:out value="${_head}" escapeXml="false"/>
        <meta name="_uniquePageId_" content="_page_"/>                                                                                              
        <script type="text/javascript">
            $(document).ready(function() {
                // bring the page to the top, so that when people refresh when reading the bottom of
                // the wiki syntax tab, they still go to the top of the page.
                document.location.href = document.location.href+"#";
            });
        </script>

        <jsecurity:hasPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
        <script type="text/javascript">
        $(document).ready(function() {
            $("#hideComments").click(function() {
                $("#commentsDiv").toggle(false);
            });
            $("#showComments").click(function() {
                $("#commentsDiv").toggle(true);
            });
        });
        </script>
        </jsecurity:hasPermission>
        <jsecurity:lacksPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
            Lacks "space:viewPageAsWiki:${actionBean.page.space.id}" permission
        </jsecurity:lacksPermission>
    </stripes:layout-component>


    <!-- view -->
    <stripes:layout-component name="_content_1">
        <a id="TOP_OF_PAGE" href="#"></a>
        <jsecurity:hasPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
        <p/>
        <p/>
        <stripes:useActionBean alwaysExecuteEvent="true"
                                beanclass="foo.bar.wiki.web.action.page.PageActionBean"
                                event="preparePageContents"
                                executeResolution="false"
                                validate="false"
                                var="_pageContent"/>

        <c:out value="${_pageContent.renderedContent}" escapeXml="false"/>
        <p/>
        <p/>
        <p/>
        <h3>Comments (${_pageContent.page.totalComments}) </h3> [ <a id="showComments" href="#">Show</a> / <a id="hideComments" href="#">Hide</a> ]
        <jsecurity:hasPermission name="space:commentPage:${actionBean.page.space.id}">
        <div id="commentsDiv">
        <stripes:form beanclass="foo.bar.wiki.web.action.comment.AddCommentActionBean" method="POST">
            <stripes:errors/>
            <stripes:hidden name="page" value="${_pageContent.page.id}"/>
            <stripes:textarea name="comment.content" style="width:100%; height:100%;" rows="10" />
            <br/>
            <img src="<c:url value='/kaptcha.jpg'/>"/> <stripes:text name="kaptcha" />
            <br/>
            <stripes:submit name="save">Save</stripes:submit>
        </stripes:form>
        <p/>
        </jsecurity:hasPermission>
        <c:forEach items="${_pageContent.page.comments}" var="comment" varStatus="stat">
            <div class="box">
                <c:out value="${comment.renderedContent}" escapeXml="false"/>
                <p>
                    <b>Posted by</b>
                    <c:choose>
                        <c:when test="${empty comment.creator}">
                            <%=UserAccountRealm.ANONYMOUS_USER_ACCOUNT.getUser().getUsername()%>
                        </c:when>
                        <c:otherwise>
                            ${comment.creator.username}
                        </c:otherwise>
                    </c:choose>
                    <b>on</b> <fmt:formatDate value="${comment.creationDate}" pattern="dd/MM/yyyy hh:mm:ss a"/>
                    <jsecurity:hasPermission name="space:editSpace:${actionBean.page.space.id}">
                    [
                        <stripes:link beanclass="foo.bar.wiki.web.action.comment.RemoveCommentActionBean" event="remove">
                            <stripes:param name="comment" value="${comment.id}"/>
                            Delete
                        </stripes:link>
                    ]
                    </jsecurity:hasPermission>
                </p>
            </div>
            <p/>
        </c:forEach>
        </div>
        <p/>
        </jsecurity:hasPermission>
        <jsecurity:lacksPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
            Lacks "space:viewPageAsWiki:${actionBean.page.space.id}" permission
        </jsecurity:lacksPermission>
    </stripes:layout-component>

    <!-- edit wiki syntax -->
    <stripes:layout-component name="_content_2">
        <jsecurity:hasPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
        <stripes:form beanclass="foo.bar.wiki.web.action.page.PageActionBean" method="POST">
            <stripes:errors />

            <stripes:hidden id="pageId" name="page" value="${actionBean.page.id}"/>
            <stripes:textarea id="wikiMarkup" style="width:100%; height:100%;" rows="18" name="page.content" value="${actionBean.page.content}"/>
            <br/>
            <stripes:submit name="save">Save</stripes:submit>
        </stripes:form>
        </jsecurity:hasPermission>
        <jsecurity:lacksPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
            Lacks "space:viewPageAsWiki:${actionBean.page.space.id}" permission
        </jsecurity:lacksPermission>
    </stripes:layout-component>

    <!-- preview -->
    <stripes:layout-component name="_content_3">
    </stripes:layout-component>

    <!-- resources -->
    <stripes:layout-component name="_content_4">
        <jsecurity:hasPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
        <displaytag:table id="element" name="${actionBean.page.resources}"
                          style="width:100%;"
                          excludedParams="*"
                          defaultsort="1"
                          defaultorder="ascending"
                          requestURI=""
                          requestURIcontext="true">
            <displaytag:column property="name" sortable="false" sortName="name" title="Name"/>
            <displaytag:column property="mimeType" sortable="false" sortName="description" title="Description"/>
            <displaytag:column property="creator.username" sortable="false" sortName="creator" title="Creator"/>
            <displaytag:column property="creationDate" sortable="false" sortName="creationDate"
                                  title="Creation Date"/>
            <displaytag:column property="modifier.username" sortable="false" sortName="modifier" title="Modifier"/>
            <displaytag:column property="modificationDate" sortable="false" sortName="modificationDate"
                                  title="Modification Date"/>
            <displaytag:column title="Actions">
                <stripes:link beanclass="foo.bar.wiki.web.action.resource.EditResourceActionBean" event="view">
                    <stripes:param name="resource" value="${element.id}"/>
                    Edit
                </stripes:link>|
                <stripes:link beanclass="foo.bar.wiki.web.action.resource.RemoveResourceActionBean"
                              event="remove_backToPageWikiPage">
                    <stripes:param name="resource" value="${element.id}"/>
                    Delete
                </stripes:link>
            </displaytag:column>
        </displaytag:table>
        </jsecurity:hasPermission>
        <jsecurity:lacksPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
            Lacks "space:viewPageAsWiki:${actionBean.page.space.id}" permission
        </jsecurity:lacksPermission>
    </stripes:layout-component>

    <!-- wiki syntax (help) -->
    <stripes:layout-component name="_content_5">
        <jsecurity:hasPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
        <stripes:url var="url" beanclass="foo.bar.wiki.web.action.general.WikiSyntaxHelpActionBean" event="view" prependContext="false" />
        <c:import url="${url}"/>
        </jsecurity:hasPermission>
        <jsecurity:lacksPermission name="space:viewPageAsWiki:${actionBean.page.space.id}">
            Lacks "space:viewPageAsWiki:${actionBean.page.space.id}" permission
        </jsecurity:lacksPermission>
    </stripes:layout-component>

</stripes:layout-render>



